/*
*  Copyright (c) 2018. 玺得（深圳）科技有限公司.保留所有权利. http://www.pi-top.com/ 玺得（深圳）科技有限公司保留所有代码著作权.如有任何疑问请访问官方网站与我们联系. 代码只针对特定需求定制编码，不得在未经允许或授权的情况下对外传播扩散.恶意传播者，法律后果自行承担. 本代码仅用于pps-cloud系统.
*/
package com.pitop.order.service;

import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.exceptions.OrderException;
import com.pitop.order.api.OrderFeignApi;
import com.pitop.order.entity.Order;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 订单
 *
 * @author berton
 */
@Service("orderSVImpl")
@Slf4j
public class OrderSVImpl {

    @Autowired
    private OrderFeignApi orderFeignApi;

    /**
     * 加载一个对象Order
     *
     * @param id * @param code 编码
     * @return Order
     */
    public Order load(Long id, String code) {
        if (id == null && code == null) {
            throw new OrderException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return orderFeignApi.load(id, code);
    }

    /**
     * 加载一个对象Order 通过id
     *
     * @param id
     * @return Order
     */
    public Order loadById(Long id) {
        if (id == null) {
            throw new OrderException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return orderFeignApi.loadById(id);
    }

    /**
     * 加载一个对象Order 通过code
     *
     * @param code 编码
     * @return Order
     */
    public Order loadByCode(String code) {
        if (code == null) {
            throw new OrderException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        return orderFeignApi.loadByCode(code);
    }


    /**
     * 删除对象Order
     *
     * @param id * @param code 编码
     */

    public void delete(Long id, String code) {
        if (id == null && code == null) {
            throw new OrderException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        orderFeignApi.delete(id, code);
    }


    /**
     * 查询Order分页
     *
     * @param order    对象
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Order>
     */
    public List<Order> list(Order order, int curPage, int pageSize) {
        return orderFeignApi.list(order, curPage, pageSize);
    }

    /**
     * 查询Order分页
     *
     * @param id       * @param code  编码
     * @param curPage  当前页
     * @param pageSize 分页条数
     * @return List<Order>
     */
    public List<Order> list(Long id, String code, int curPage, int pageSize) {
        if (curPage < 0) {
            curPage = 1;
        }

        if (pageSize < 0) {
            pageSize = Page.limit;
        }

        Order order = new Order();
        order.setId(id);
        order.setCode(code);

        return orderFeignApi.list(order, curPage, pageSize);
    }


    public int count(Long id, String code) {
        return orderFeignApi.count(id, code);
    }


    public int count(Order order) {
        return orderFeignApi.count(order);
    }


    /**
     * 保存
     *
     * @param order 实体
     * @throws BaseException
     */
    public void save(Order order) {
        if (order == null) {
            throw new OrderException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        orderFeignApi.build(order);
    }

    /**
     * 更新
     *
     * @param order 实体
     * @throws BaseException
     */
    public void modify(Order order) {
        if (order == null) {
            throw new OrderException(BaseException.BaseExceptionEnum.Illegal_Param);
        }
        orderFeignApi.modify(order);
    }


}
